package com.example.email.controller;

import com.example.email.common.R;
import com.example.email.entity.MailVo;
import com.example.email.service.MailService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.internet.MimeMessage;
@Slf4j
@RestController
@RequestMapping("email")
public class EmailController {

    @Autowired
    private MailService mailService;
    @GetMapping("sentEmail")
    public void sendMail(){
        MailVo mailVo=new MailVo();
        String author="lee";
        String mail="1531451260@qq.com";
        String text="this is mail content";

        mailVo.setFrom("2452563437@qq.com");
        mailVo.setTo("1531451260@qq.com");
        mailVo.setSubject("收到您朋友【"+author+"】的信件,Ta的邮箱:"+mail);
        mailVo.setText(text);

        mailService.sendMail(mailVo);

    }

    @GetMapping("sentEmail/{content}/{recipient}/{subject}")
//    @ApiImplicitParams({@ApiImplicitParam(name = "subject",value = "subject" , dataType = "String" ),
//            @ApiImplicitParam(name = "content",value = "content", dataType = "String",required = true),
//          })

    public R sendMailByMessage(@PathVariable String content , @PathVariable String recipient , @PathVariable  String subject ){

        MailVo mailVo = new MailVo();
        if (content=="content"){
            return R.ok().message("content is null");
        }else {
            mailVo.setText(content);
        }
       if (recipient==null){
           return R.error().message("recipient is null");
       }else {
           mailVo.setTo(recipient);
       }

        mailVo.setFrom("lesin007@163.com");

        if (subject=="{subject}"){
            mailVo.setSubject("-----this mail is no subject");
        }else {
            mailVo.setSubject(subject);
        }


        mailService.sendMail(mailVo);
        return R.ok().data("mailVo",mailVo);
    }

    @GetMapping("sentEmailByCode/{recipient}/{code}")
    public R sentEmailByCode(@PathVariable String recipient ,@PathVariable int code){
        MailVo mailVo = new MailVo();
        mailVo.setText("您的验证码为: "+code+" ,请在---秒内使用 ");

        if (recipient==null){
            return R.error().message("recipient is null");
        }else {
            mailVo.setTo(recipient);
        }

        mailVo.setFrom("lesin007@163.com");

        mailVo.setSubject(" ****验证码****");
        mailService.sendMail(mailVo);

      return   R.ok().data("mailVo",mailVo).data("code",code);
    }

}
